package com.zja.dao.Impl;

import com.zja.dao.BaseDAO;
import com.zja.dao.GroupChatDao;
import com.zja.model.GroupChat;

import java.util.List;

/**
 * @author JA
 */
public class GroupChatDaoImpl extends BaseDAO implements GroupChatDao {
    @Override
    public List<GroupChat> selectAll() {
        String sql = "SELECT * FROM group_chat";
        try {
            return executeQuery(GroupChat.class, sql);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public GroupChat selectById(Long id) {
        try {
            String sql = "SELECT * FROM group_chat WHERE id = ?";
            return executeQueryBean(GroupChat.class, sql, id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean insert(GroupChat groupChat) {
        try {
            String sql = "INSERT INTO group_chat (name, creator_id, created_at) VALUES (?, ?, ?)";
            return executeUpdate(sql, groupChat.getName(), groupChat.getCreatorId(), groupChat.getCreatedAt());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean update(GroupChat groupChat) {
        try {
            String sql = "UPDATE group_chat SET name = ?, creator_id = ? WHERE id = ?";
            return executeUpdate(sql, groupChat.getName(), groupChat.getCreatorId(),  groupChat.getId());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean deleteById(Long id) {
        try {
            String sql = "DELETE FROM group_chat WHERE id = ?";
            return executeUpdate(sql, id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
